package excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.write.metadata.WriteSheet;
import pojo.Student;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class excelUtil {

    private static final int MAX_COUNT_PRE_SHEET = 50;

    /*public static void main(String[] args) throws FileNotFoundException {
        List<Student> list = new ArrayList<Student>();
        for(int i=0;i<200;i++){
            Student stu = new Student();
            stu.setName("张三"+i);
            stu.setAge(10+i);
            stu.setGender(i%2==0? "男":"女");
            stu.setClassname("软工一班");
            list.add(stu);
        }
        OutputStream os = new FileOutputStream("D:\\JavaWeb\\WebDemo4\\src\\excel\\学生信息表.xlsx");
        exportExcel(os,Student.class,"用户信息表",list);
//        String excelPath = "D:\\JavaWeb\\WebDemo4\\src\\excel\\学生信息表.xlsx";
//        generateExcel(excelPath,"用户信息表2",list,Student.class);
//        readExcel(excelPath,"用户信息表2",Student.class);
    }*/

    public static <T> List<T> importExcel(InputStream is, Class<T> clazz){
        List<T> dataList = new ArrayList<T>();
        ReadListener<T> listener = new ReadListener<T>() {
            @Override
            public void invoke(T t, AnalysisContext analysisContext) {
                dataList.add(t);
//                System.out.println("读取了一行数据:"+t.toString());
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//                System.out.println("excel读取一个sheet完毕");
//                System.out.println("读取的行数:"+dataList.size());
            }
        };
        EasyExcel.read(is,clazz,listener).doReadAll();
        return dataList;
    }

    public static <T> void exportExcel(OutputStream os,Class<T> clazz, String sheetName,List<T> dataList){
        ExcelWriter writer = EasyExcel.write(os, clazz).build();
        int size = dataList.size();
        int sheetCount = size / MAX_COUNT_PRE_SHEET;
        if(size % MAX_COUNT_PRE_SHEET != 0) sheetCount++;
        for (int i = 0; i < sheetCount; i++) {
            int start = i * MAX_COUNT_PRE_SHEET;
            int end = Math.min((start+1)*MAX_COUNT_PRE_SHEET,size);
            List<T> sheetData = dataList.subList(start,end);
            WriteSheet sheet = new WriteSheet();
            sheet.setSheetNo(i);
            sheet.setSheetName(sheetName+"-"+i);
            writer.write(sheetData,sheet);
        }
        writer.finish();
    }

   /* private static <T> List<T> readExcel(String excelPath,String sheetName,Class<T> clazz) {
        List<T> dataList = new ArrayList<T>();
        ReadListener<T> listener = new ReadListener<T>() {
            @Override
            public void invoke(T t, AnalysisContext analysisContext) {
                dataList.add(t);
                System.out.println("读取了一行数据:"+t.toString());
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("excel读取一个sheet完毕");
                System.out.println("读取的行数:"+dataList.size());
            }
        };
        EasyExcel.read(excelPath,clazz,listener).sheet(sheetName).doRead();
        return dataList;
    }

    public static <T> void generateExcel(String excelPath,String sheetName,List<T> list,Class<T> clazz){
        EasyExcel.write(excelPath,clazz).sheet(sheetName).doWrite(list);
    }
*/
}
